<?php

namespace Platform\Api;

use PhalApi\Api;
use Platform\Domain\Admin as AdminDomain;
use PhalApi\Exception\InternalServerErrorException;

/**
 * 后台用户模块
 */
class Admin extends Api {
    
    public function getRules() {
        return array(
            'getInfo' => array(
            ),
            'logout'=> array(
            ),
            'userList' => array(
                'page' => array('name' => 'page', 'type' => 'int', 'default' => 1, 'min' => 1, 'desc' => \PhalApi\T('page')),
                'limit' => array('name' => 'limit', 'type' => 'int', 'default' => 20, 'min' => 1, 'max' => 1000, 'desc' => \PhalApi\T('page limit')),
                'name' => array('name' => 'name', 'require' => false, 'desc' => '用户名称'),
                'sort' => array('name' => 'sort', 'require' => false, 'desc' => 'id排序值'),
            ),
            'addUser'=> array(
                'name' => array('name' => 'name', 'require' => true, 'desc' => '用户名称'),
                'avatar' => array('name' => 'avatar', 'require' => false, 'desc' => '用户头像'),
                'password' => array('name' => 'password', 'require' => true, 'desc' => '用户密码'),
                'role_id' => array('name' => 'role_id', 'require' => true, 'desc' => '所选角色'),
            ),
            'userDetail'=> array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '用户id'),
            ),
            'updateUser'=> array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '用户id'),
                'name' => array('name' => 'name', 'require' => true, 'desc' => '用户名称'),
                'avatar' => array('name' => 'avatar', 'require' => false, 'desc' => '用户头像'),
                'password' => array('name' => 'password', 'require' => false, 'desc' => '用户密码'),
                'role_id' => array('name' => 'role_id', 'require' => true, 'desc' => '所选角色'),
            ),
            'updatePass'=> array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '用户id'),
                'password' => array('name' => 'password', 'require' => true, 'desc' => '用户密码'),
            ),
            'deleteUser'=> array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '用户id'),
            ),
        );
    }

    /**
     * 获取用户信息
     * @desc 根据token获取用户信息
     */
    public function getInfo() {
        return \PhalApi\DI()->platformAdmin->check();
    }

    /**
     * 退出登录
     * @desc 退出当前管理的登录
     */
    public function logout() {
        \PhalApi\DI()->platformAdmin->logout();
        return array('is_logout' => true);
    }

    /**
     * 获取角色列表
     * @desc 获取角色列表
     */
    public function userList() {
        $domain = new AdminDomain();
        $where = array();
        //分页
        $page = $this->page? $this->page:1;
        $perpage = $this->limit?$this->limit:10;
        //要查询的字段
        $select="id,role_id,name,from_unixtime(add_time, '%Y-%m-%d %H:%i:%S') as add_time,add_who,avatar";
        //处理条件查询
        if( isset($this->name)&&$this->name != '' ){
            $where['name like ?'] = "%".$this->name."%";
        }
        //处理排序
        $order='id DESC';
        $roles = $domain->userList($where,$select,$order,$page,$perpage);
        return $roles;
    }

    /**
     * 添加用户接口
     * @desc 添加用户接口
     * @return int user_id 用户ID
     */
    public function addUser() {
        $domain = new AdminDomain();
        //列出所有需要添加的参数
        $insertData = array();
        $insertData['name'] = $this->name;
        $insertData['avatar'] = $this->avatar?$this->avatar:'';
        $insertData['password'] = $this->password;
        $insertData['role_id'] = $this->role_id;
        $rs = $domain->addUser($insertData);
        return $rs;
    }

    /**
     * 获取用户详情
     * @desc 获取用户详情
     */
    public function userDetail() {
        $domain = new AdminDomain();
        $id = $this->id;
        $select='id,name,role_id,avatar';
        return $domain->userDetail($id,$select);
    }

    /**
     * 编辑提交用户
     * @desc 编辑提交用户
     */
    public function updatePass() {
        $domain = new AdminDomain();
        //列出所有需要添加的参数
        $id = $this->id;
        $updateData = array();
        $updateData['password'] = $this->password;
        return $domain->updatePass($id,$updateData);
    }
    /**
     * 编辑提交用户密码
     * @desc 编辑提交用户密码
     */
    public function updateUser() {
        $domain = new AdminDomain();
        //列出所有需要添加的参数
        $id = $this->id;
        $updateData = array();
        if( isset($this->password) && $this->password != '' ){
            $updateData['password'] = $this->password;
        }
        $updateData['name'] = $this->name;
        $updateData['avatar'] = $this->avatar?$this->avatar:'';
        $updateData['role_id'] = $this->role_id;
        return $domain->updateUser($id,$updateData);
    }
    /**
     * 删除角色
     * @desc 删除角色
     */
    public function deleteUser() {
        $domain = new AdminDomain();
        $id = $this->id;
        return $domain->deleteUser($id);
    }
}
